package com.top.ruizhong.popup

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.LogUtils
import com.top.ruizhong.R
import com.top.ruizhong.main.access.CodeFragment
import kotlinx.android.synthetic.main.fragment_access_code.*
import kotlinx.android.synthetic.main.popup_password_verify.view.*
import razerdp.basepopup.BasePopupWindow

class PasswordVerifyPopup(
    context: Context, val onForget: () -> Unit,
    val onConfirm: (password: String) -> Unit,
    val onFinish: () -> Unit
) : BasePopupWindow(context) {


    private val ARRAY_CODE: Array<String?> = arrayOfNulls(6)

    init {
        setOverlayNavigationBar(false)
        setBackPressEnable(false)
        setAdjustInputMethod(false)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_password_verify)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        BarUtils.addMarginTopEqualStatusBarHeight(contentView.flPwdVerifyBar)
        contentView.tvPwdVerifyForget.setOnClickListener {
            onForget()
        }

        contentView.etCheckCodeContent?.let {
            it.isLongClickable = false
            it.setTextIsSelectable(false)
        }

        contentView.etCheckCodeContent?.setOnClickListener { v ->
            val editText: EditText = v as EditText
            editText.setSelection(editText.text.length)
            editText.isCursorVisible = false
        }

        contentView.etCheckCodeContent?.setOnTouchListener { v, event ->
            if (event.action == android.view.MotionEvent.ACTION_UP) {
                val editText: EditText = v as EditText
                editText.setSelection(editText.text.length)
                editText.isCursorVisible = false
            }
            return@setOnTouchListener false
        }

        contentView.etCheckCodeContent.addTextChangedListener {
            val password: String? = it?.toString()
            fillDataCode(code = password ?: "")
            if (password == null || password.length != 6)
                return@addTextChangedListener
            onConfirm(password)
        }

    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    override fun showPopupWindow() {
        super.showPopupWindow()
        KeyboardUtils.showSoftInput(contentView.etCheckCodeContent)
    }


    /**
     * 设置验证码数据
     * */
    private fun fillDataCode(code: CharSequence) {
        val length: Int = code.length
        for (i: Int in (if (length == 0) 0 else length - 1)
                until ARRAY_CODE.size) {
            ARRAY_CODE[i] = null
        }
        for (i: Int in 0 until length) {
            ARRAY_CODE[i] = code.toList()[i].toString()
        }
        if (length != 0) {
            ARRAY_CODE[length - 1] = code.last().toString()
        }
        contentView.tvCheckCode1.text = ARRAY_CODE[0]
        contentView.tvCheckCode2.text = ARRAY_CODE[1]
        contentView.tvCheckCode3.text = ARRAY_CODE[2]
        contentView.tvCheckCode4.text = ARRAY_CODE[3]
        contentView.tvCheckCode5.text = ARRAY_CODE[4]
        contentView.tvCheckCode6.text = ARRAY_CODE[5]

        if (code.length == 6) {
            KeyboardUtils.hideSoftInput(contentView.etCheckCodeContent)
        }
    }

    override fun onBackPressed(): Boolean {
        onFinish()
        return false
    }

}